import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JScrollPane;

import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.util.Scanner;

import org.antlr.v4.codegen.model.SrcOp;

import antlr.non_ho_niente_da_mettermiLexer;
import antlr.non_ho_niente_da_mettermiParser;
import antlr.non_ho_niente_da_mettermiParser.*;


public class Test extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private String filePath = null;
	final JTextArea textArea;
	final JTextArea textArea_1;
	Boolean firstElement = false;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Test frame = new Test();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	private void saveFileDialog()
	{
		JFileChooser saveFile = new JFileChooser(filePath);
		saveFile.addChoosableFileFilter(new FileNameExtensionFilter("File Dress (.dress)", "dress"));
		saveFile.setFileFilter(saveFile.getChoosableFileFilters()[1]);
		int returnVal= saveFile.showSaveDialog(null);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			try {
				String path = saveFile.getSelectedFile().getCanonicalPath();
				FileWriter logg;
				logg = new FileWriter(new File(path+".dress"), false);
				filePath = path+".dress";
				textArea.write(logg);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

	/**
	 * Create the frame.
	 */
	public Test() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 685, 900);
		setTitle("Non ho niente da mettermi IDE");
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 10, 664, 484);
		contentPane.add(scrollPane);
		
		textArea = new JTextArea();
		scrollPane.setViewportView(textArea);
		textArea.setColumns(55);
		textArea.setRows(30);

		
	
		
		JButton btnSaveAs = new JButton("Save As");
		btnSaveAs.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				saveFileDialog();
			}
		});
		btnSaveAs.setBounds(330, 499, 117, 29);
		contentPane.add(btnSaveAs);
		
		JButton btnOpen = new JButton("Open");
		btnOpen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser openFile = new JFileChooser();
				openFile.addChoosableFileFilter(new FileNameExtensionFilter("File Dress (.dress)", "dress"));
				openFile.setFileFilter(openFile.getChoosableFileFilters()[1]);
				int returnVal= openFile.showOpenDialog(null);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					  File file = openFile.getSelectedFile();
					  try {
						filePath = file.getCanonicalPath();
						String content = new Scanner(file).useDelimiter("\\Z").next();
						textArea.setText(content);
					} catch (FileNotFoundException e1) {
						e1.printStackTrace();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			}
		});
		btnOpen.setBounds(110, 499, 117, 29);
		contentPane.add(btnOpen);
		
		JButton btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					if(filePath == null)
					{
						saveFileDialog();
					}
					else
					{
						FileWriter logg = new FileWriter(new File(filePath), false);
						textArea.write(logg);
					}
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});
		btnSave.setBounds(220, 499, 117, 29);
		contentPane.add(btnSave);
		
		JButton btnRun = new JButton("Run");
		btnRun.addActionListener(new ActionListener()   {
			public void actionPerformed(ActionEvent e)  {
		        try {
					ANTLRInputStream input = new ANTLRInputStream(new StringReader(textArea.getText()));
					non_ho_niente_da_mettermiLexer lexer = new non_ho_niente_da_mettermiLexer(input);
			        CommonTokenStream tokens = new CommonTokenStream(lexer);
			        non_ho_niente_da_mettermiParser parser = new non_ho_niente_da_mettermiParser(tokens);
			        ParseTree tree = parser.scope();

			        non_ho_niente_da_mettermiVisitor loader = new non_ho_niente_da_mettermiVisitor();
			        loader.setErrorReporter(new SemanticErrorHandler());
			        loader.visit(tree);
			       
			        StringBuilder sb = new StringBuilder();
			        sb.append("These are the suggested outfits.");
			        	
			        sb.append("\n");
			       	for(String s: non_ho_niente_da_mettermiVisitor.outfitSet)
			       	{
			       		sb.append("\n"+s);
			       		sb.append("\n-----------------\n");
			       	}
		        	textArea_1.setForeground(Color.BLACK);
			        textArea_1.setText(sb.toString());
			        textArea_1.setCaretPosition(0);
			        
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		        catch (Exception e2) {
		        	textArea_1.setForeground(Color.RED);
		        	textArea_1.setText(e2.getMessage());
				}

			}
		});
		btnRun.setBounds(440, 499, 117, 29);
		contentPane.add(btnRun);
		
		JButton btnShow = new JButton("Show Theory");
		btnShow.addActionListener(new ActionListener()   {
			public void actionPerformed(ActionEvent e)  {
				JFrame frame = new JFrame();
				JScrollPane scrollPane = new JScrollPane();
				scrollPane.setBounds(10, 10, 664, 484);
				frame.add(scrollPane);
				
				JTextArea text = new JTextArea(non_ho_niente_da_mettermiVisitor.t.toString());
				scrollPane.setViewportView(text);
				text.setColumns(55);
				text.setRows(30);
				frame.setTitle("Prolog Theory");
				
				frame.pack();
				frame.setVisible(true);
			}
		});
		btnShow.setBounds(550, 499, 117, 29);
		contentPane.add(btnShow);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(10, 533, 664, 260);
		contentPane.add(scrollPane_1);
		
		textArea_1 = new JTextArea();
		textArea_1.setEditable(false);
		scrollPane_1.setViewportView(textArea_1);
		textArea_1.setRows(15);
		textArea_1.setColumns(45);
		
		JButton btnNew = new JButton("New");
		btnNew.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textArea.setText(""); 
				filePath = null;
			}
		});
		btnNew.setBounds(0, 499, 117, 29);
		contentPane.add(btnNew);
	}
}
